<?php use_javascripts_for_form($form) ?>
<?php use_stylesheet('hoverTables.css') //load css of tables  ?>
<?php use_stylesheet('forms.css') //load css pf order page   ?>

<?php
//Session functions
    
//Retrieve the hidden variables and put them in an array
//Then put the array in the session
function setVariables($id){
    $array['id'] = $id;
    $array['number'] = $_POST['number']; 
    $array['amount'] = $_POST['amount'];
    $array['name'] = $_POST['name'];
    $array['unit'] = $_POST['unit'];
    $array['type'] = $_POST['type'];
    $array['price'] = $_POST['price'];
    $array['stock'] = $_POST['stock'];
    $array['minstock'] = $_POST['minstock'];
    $array['supplier'] = $_POST['supplier'];
    $array['status'] = $_POST['status'];
        
    $i = 0;
    while(isset($_SESSION['purchaselist'][$i])){                  
        $i++;
    } 
    $_SESSION['purchaselist'][$i] = $array;
    unset($_POST['add']);
} 

if(isset($_POST['empty_purchase'])){
    if(isset($_SESSION['purchaselist'])){
        unset($_SESSION['purchaselist']);
    }
}
if(isset($_POST['removep'])){
    $idline = $_POST['removep'];
    unset($_SESSION['purchaselist'][$idline]);
}
if(isset($_POST['addp'])) {
    //check if something is filled in in amount, if not don't do anything
    if(!empty($_POST['amount'])){
        $id = $_POST['id'];
        //check if part already exists in session
        //only add part when its not in session yet
        if(isset ($_SESSION['purchaselist'])){
            $inlist = false;
            foreach($_SESSION['purchaselist'] as $value){                 
                if($id == $value['id']){
                    $inlist = true;
                    break;
                }
            }
            if($inlist == false){
                setVariables($id);
            }
        } else{
            setVariables($id);
        }
    }
    unset($_POST['add']);
}
if(isset($_POST['update'])){
    $i = 0;
    while(isset($_POST[''.$i.''])){
        $_SESSION['purchaselist'][$i]['amount'] = $_POST['amount'.$i.''];
        $_SESSION['purchaselist'][$i]['price'] = $_POST['price'.$i.''];
        $i++;
    }    
    unset($_POST['update']);
    }
?>
<div id="form"> 
    <!-- Back button -->
    <a href="<?php echo url_for('purchase/index') ?>" class="bt_back"><img src="<?php echo image_path('Buttons/bt_arrow_back.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_arrow_back_hov.png') ?>'" onmouseout="this.src='<?php echo image_path('Buttons/bt_arrow_back.png') ?>'"></a>
    <!-- The actual form -->
    <form action="<?php echo url_for('purchase/'.($form->getObject()->isNew() ? 'createPurchase' : 'updatePurchase').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
        <fieldset>
            <legend class="bold">Materials</legend>
            <table class="table_top" style="margin-left: 0;">
                <tr class="bold head_small">
                    <td width="65px">Material No.</td>
                    <td>Name</td>
                    <td>Type</td>
                    <td>Unit</td>
                    <td width="35px">Stock</td>
                    <td width="55px">Minimum</td>
                    <td width="45px">Ordered</td>
                    <td width="43px">Quantity</td>
                    <td width="105px">&nbsp;Supplier</td>                    
                    <td width="70px">Price per Unit (excl. VAT)</td>
                    <td width="75px"></td>
                    <td width="35px"></td>
                </tr>
            </table>
            <div id="scroll_div">
                <table id="purchase">
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="visibility:hidden;">
                        <input type="hidden" value="Add" name="retp"/>
                    </form>
                    <tr class="bold head_small" height="15px">
                        <td width="65px"></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td width="35px"></td>
                        <td width="55px"></td>
                        <td width="45px"></td>
                        <td width="43px"></td>
                        <td width="105px"></td>
                        <td width="70px"></td>
                        <td width="75px"></td>
                        <td width="20px"></td>
                    </tr>
                    <?php processList($needToPurchase, $form); ?>
                    <?php processList($materials, $form); ?>
                </table>
            </div>
        </fieldset>
<?php if(!empty($_SESSION['purchaselist'])){ //Check if session has been set ?>
        <fieldset>
            <legend class="bold">Purchase</legend>
            <div class="table_div">
            <table id="purchaselist">
                <tr class="bold head_small">
                    <td>Material No.</td>
                    <td>Name</td>
                    <td>Type</td>
                    <td>Supplier</td>
                    <td>Quantity</td>
                    <td>Unit</td>
                    <td>Price per Unit (excl. VAT)</td>
                    <td>Status</td>
                    <td width="20px"></td>
                </tr>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                    <input type="submit" name="empty_purchase" value="" class="bt_empty" title="Empty entire purchase list"/>
                </form>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                    <input type="submit" name="update" value="" class="bt_update" title="Update purchase list"/>
                    <?php
                    foreach($_SESSION['purchaselist'] as $value){ ?>
                        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> "><?php
                        echo '<td>'.$value['number'].'</td>';
                        echo '<td>'.$value['name'].'</td>';
                        echo '<td>'.$value['type'].'</td>';
                        $supplier = ContactTable::getInstance()->findOneBy('id', $value['supplier']);
                        if($supplier == null){
                            echo '<td>'.$value['supplier'].'</td>';
                        }
                        else{
                            echo '<td>'.$supplier->getName().'</td>';
                        } 
                        echo '<input type="hidden" name="id" value="'.$value['id'].'"/>';
                        echo '<input type="hidden" name="name" value="'.$value['name'].'"/>';
                        echo '<input type="hidden" name="price" value="'.$value['price'].'"/>';
                        echo '<input type="hidden" name="type" value="'.$value['type'].'"/>';
                        echo '<input type="hidden" name="number" value="'.$value['number'].'"/>';
                        echo '<input type="hidden" name="stock" value="'.$value['stock'].'"/>';
                        echo '<input type="hidden" name="minstock" value="'.$value['minstock'].'"/>';
                        echo '<input type="hidden" name="'.array_search($value, $_SESSION['purchaselist']).'" value="'.array_search($value, $_SESSION['purchaselist']).'"/>';
                        echo '<td><input type="text" value="'.$value['amount'].'" name="amount'.array_search($value, $_SESSION['purchaselist']).'" size="3"/></td>';
                        echo '<td>'.$value['unit'].'</td>';
                        echo '<td><input type="text" name="price'.array_search($value, $_SESSION['purchaselist']).'" size="5" value="'.$value['price'].'"/></td>';
                        echo '<td>'.$value['status'].'</td>';
                        echo '<input type="hidden" name="status" value="'.$value['status'].'"/>';
                        echo '<td><input type="submit" value="'.array_search($value, $_SESSION['purchaselist']).'" name="removep" class="bt_delete"/></td>';
                        echo '</tr>';
                    }
                    ?>
                </form>
                
            </table>
            </div>
        </fieldset>
        <?php if (!$form->getObject()->isNew()): ?>
            &nbsp;<?php echo link_to('Delete', 'sales/deletePurchase?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
        <?php endif; ?>
        <div class="hidden_csrf"><?php echo $form['_csrf_token']->renderRow(); ?></div>
        <input type="submit" value="" class="ok bt_margin"/>
        <div style="margin: -35px 0 0 80px;">
        <!-- Print Button -->
        <a href="<?php echo url_for('purchase_show_received') ?>" target="_blank">
            <img src="<?php echo image_path('Buttons/bt_print.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_print_hov.png') ?>';" onmouseout="this.src='<?php echo image_path('Buttons/bt_print.png') ?>';" title="Show Received List"/>
        </a>
        </div>
        <?php } ?>
    </form>    
</div> 

<!-- Retrieve all the data of each material -->
<?php function processList($list, $form){ 
     foreach ($list as $i=>$material): ?>
        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <td><?php echo $material->getPartNumber() ?></td>
                <td><?php echo $material->getName() ?></td>
                <td><?php echo $material->getType() ?></td>
                <td><?php echo $material->getUnit() ?></td>
                <td><?php echo $material->getStock() ?></td>
                <td><?php echo $material->getMinStock() ?></td>
                <!-- Retrieve the total amount this material already has been ordered -->
                <?php $ordered = PurchaseTable::getAmountOrderedMaterial($material) ?>
                <td><?php foreach($ordered as $purchase){
                    if($purchase->getAmount() != null){
                        echo $purchase->getAmount(); 
                    } else{ 
                        echo '0'; 
                    } 
                }?></td>
                <td><input type="text" name="amount" size="3" value="0"/></td>
                <!-- Retrieve all the suppliers from the contact table and show these in a drop down box -->
                <?php $suppliers = ContactTable::getInstance()->findBy('category_name', 'supplier') ?>
                <td>
                    <select name="supplier">
                        <option value="<?php echo null ?>"></option>
                            <?php foreach($suppliers as $supplier){?>
                                <option value="<?php echo $supplier->getId() ?>"><?php echo $supplier->getName() ?></option>
                            <?php } ?>
                    </select>
                </td>
                <!-- Put all the variables in hidden inputs so these that can be stored in the session -->
                <input type="hidden" name="id" value="<?php echo $material->getId() ?>"/>
                <input type="hidden" name="number" value="<?php echo $material->getPartNumber() ?>"/>
                <input type="hidden" name="name" value="<?php echo $material->getName() ?>"/>
                <input type="hidden" name="type" value="<?php echo $material->getType() ?>"/>
                <input type="hidden" name="unit" value="<?php echo $material->getUnit() ?>"/>
                <input type="hidden" name="stock" value="<?php echo $material->getStock() ?>"/>
                <input type="hidden" name="minstock" value="<?php echo $material->getMinStock() ?>"/>
                
                <td><input type="text" name="price" size="8" value="0"/></td>
                <td>
                    <select name="status">
                        <option value="received">received</option>
                        <option value="ordered">ordered</option>
                    </select>
                </td>
                <!-- The add material button -->
                <td><input type="submit" value="" name="addp" class="add_bt"/></td>
            </form>
        </tr>
<?php endforeach; 
}?>